﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Single.Utils
{
    public static class HttpHelper
    {
        /// <summary>  
        /// 创建GET方式的HTTP请求  
        /// </summary>  
        public static HttpWebResponse GetResponse(string url, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "GET";
            //设置代理UserAgent和超时
            request.UserAgent = userAgent;
            request.Timeout = timeout;

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            if (cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
            }

            return request.GetResponse() as HttpWebResponse;
        }

        /// <summary>  
        /// 创建POST方式的HTTP请求  
        /// </summary>  
        public static HttpWebResponse PostResponse(string url, string parameters, string contentType, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "POST";
            request.ContentType = contentType;

            //设置代理UserAgent和超时
            request.UserAgent = userAgent;
            request.Timeout = timeout;

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            if (cookies != null)
            {
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(cookies);
            }

            //发送POST数据  
            if (parameters != null)
            {
                byte[] data = Encoding.UTF8.GetBytes(parameters);
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }

            string[] values = request.Headers.GetValues("Content-Type");
            return request.GetResponse() as HttpWebResponse;
        }

        /// <summary>
        /// 获取请求的数据
        /// </summary>
        public static string ResponseString(WebResponse webresponse)
        {
            using (Stream s = webresponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(s, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }

        /// <summary>  
        /// 创建GET方式的HTTP请求并返回结果
        /// </summary>  
        public static string Get(string url, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            return ResponseString(GetResponse(url, timeout, userAgent, headers, cookies));
        }

        /// <summary>  
        /// 创建GET方式的HTTP请求并返回结果
        /// </summary>  
        public static string Get(string url, Dictionary<string, object> parameters, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            StringBuilder data = new StringBuilder();

            if (!url.Contains("?")) { data.Append("?"); } else { data.Append("&"); }

            foreach (var item in parameters)
            {
                data.Append(item.Key + "=" + item.Value + "&");
            }

            return ResponseString(GetResponse(url + data.ToString().TrimEnd('&'), timeout, userAgent, headers, cookies));
        }

        /// <summary>  
        /// 创建POST方式的HTTP请求并返回结果
        /// </summary>  
        public static string Post(string url, string parameters, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            return ResponseString(PostResponse(url, parameters, "application/x-www-form-urlencoded", timeout, userAgent, headers, cookies));
        }

        /// <summary>  
        /// 创建POST方式的HTTP请求并返回结果
        /// </summary>  
        public static string Post(string url, Dictionary<string, object> parameters, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            StringBuilder data = new StringBuilder();

            foreach (var item in parameters)
            {
                data.Append(item.Key + "=" + item.Value + "&");
            }

            return ResponseString(PostResponse(url, data.ToString().TrimEnd('&'), "application/x-www-form-urlencoded", timeout, userAgent, headers, cookies));
        }

        /// <summary>  
        /// 创建POST方式的HTTP请求并返回结果
        /// </summary>  
        public static string PostJson(string url, string parameters, int timeout = 60000, string userAgent = null, Dictionary<string, string> headers = null, CookieCollection cookies = null)
        {
            return ResponseString(PostResponse(url, parameters, "application/json;charset=utf-8", timeout, userAgent, headers, cookies));
        }
    }
}